package com.naiterui.ehp.bs.patient.service.impl;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bs.patient.domain.CommentStarsDeatil;
import com.naiterui.ehp.bs.patient.domain.CommentStarsInfo;
import com.naiterui.ehp.bs.patient.repository.IApCommentStarsDetailRepository;
import com.naiterui.ehp.bs.patient.repository.IApCommentStarsInfoRepository;
import com.naiterui.ehp.bs.patient.service.ICommentStarsService;
import com.naiterui.ehp.bs.patient.vo.comment.StarsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 患者评价星级
 * @Author gongbaoqiang
 * @Date 2020/6/30 5:53 PM
 */
@Service
public class ApCommentStarsServiceImpl implements ICommentStarsService {

    @Autowired
    private IApCommentStarsInfoRepository commentStarsInfoRepository;

    @Autowired
    private IApCommentStarsDetailRepository commentStarsDetailRepository;

    @Override
    public List<StarsVO> findAll() {
        Iterable<CommentStarsInfo> infos = commentStarsInfoRepository.findAll(Sort.by("level").ascending());
        Iterable<CommentStarsDeatil> details = commentStarsDetailRepository.findAll();
        Map<Integer, List<String>> detailMap = new HashMap<>();
        details.forEach(s -> {
            List<String> list = detailMap.computeIfAbsent(s.getLevel(), l -> Lists.newArrayList());
            list.add(s.getContent());
        });

        List<StarsVO> vos = new ArrayList<>();
        infos.forEach(s -> {
            vos.add(StarsVO.builder()
                    .level(s.getLevel())
                    .description(s.getDescription())
                    .contents(detailMap.get(s.getLevel()))
                    .build());
        });

        return vos;
    }
}
